package controller;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.EventBean;
import beans.UserBean;
import beans.VenueBean;

import model.EventDAO;
import model.UserDAO;


@WebServlet("/SearchUserServlet")
public class SearchUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SearchUserServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{	    
			String searchtype = request.getParameter("search"); 

			if (searchtype.equals("user")) {
				UserBean user = new UserBean();
				if (request.getParameter("searchedemail") != null)
					user.setEmail(request.getParameter("searchedemail"));
				else
					user.setEmail("");
				if (request.getParameter("searchedname") != null)
					user.setName(request.getParameter("searchedname"));
				else
					user.setName("");
				if (request.getParameter("searchedcity") != null )
					user.setCity(request.getParameter("searchedcity"));
				else
					user.setCity("");
				if (request.getParameter("searchedcountry") != null)
					user.setCountry(request.getParameter("searchedcountry"));
				else
					user.setCountry("");

				ArrayList<UserBean> searchResults = UserDAO.search(user);

				HttpSession session = request.getSession(true);
				session.setAttribute("searchResults", searchResults); 
				session.setAttribute("searchTermsUserBean", user);
				session.setAttribute("searchType", searchtype);
				response.sendRedirect("searchresults.jsp");   
				return;

			} else if (searchtype.equals("event")) {
				EventBean event = new EventBean();
				String country = "";
				String region = "";
				String city = "";
				boolean finished = false; //default as false, the event is not finished
				if (request.getParameter("searchedname") != null)
					event.setName(request.getParameter("searchedname"));
				else
					event.setName("");
				if ((request.getParameter("fromyear") != null) && (request.getParameter("frommonth") != null) && (request.getParameter("fromday") != null)) {
					String fromtimestamp = request.getParameter("fromyear") + "-" + request.getParameter("frommonth") + "-" + request.getParameter("fromday") + " " + "00:00:00";
					event.setStartTimeStamp(Timestamp.valueOf(fromtimestamp));
				}
				if ((request.getParameter("toyear") != null) && (request.getParameter("tomonth") != null) && (request.getParameter("today") != null)) {
					String totimestamp = request.getParameter("toyear") + "-" + request.getParameter("tomonth") + "-" + request.getParameter("today") + " " + "00:00:00";
					event.setEndTimeStamp(Timestamp.valueOf(totimestamp));
				}
				VenueBean venue = new VenueBean();
				if (request.getParameter("searchedcountry") != null)
					venue.setCountry(request.getParameter("searchedcountry"));
				else
					venue.setCountry("");
				if (request.getParameter("searchedregion") != null)
					venue.setRegion(request.getParameter("searchedregion"));
				else
					venue.setRegion("");
				if (request.getParameter("searchedcity") != null)
					venue.setCity(request.getParameter("searchedcity"));
				else
					venue.setCity("");
				event.setVenue(venue);

				ArrayList<EventBean> searchResultsOne = EventDAO.search(event);
				ArrayList<EventBean> searchResultsTwo = new ArrayList<EventBean>();
				for (EventBean eventone : searchResultsOne) {
					eventone = EventDAO.getEvent(eventone);
					UserBean host = new UserBean();
					host.setEmail(eventone.getHost());
					eventone.setHostName(UserDAO.getName(host));
					eventone.setGuestList(EventDAO.getGuests(eventone));
					if (request.getParameter("full") == null)
						if (request.getParameter("empty") == null)
							searchResultsTwo.add(eventone);		
						else {
							if (eventone.getGuestList().size() > 0)
								searchResultsTwo.add(eventone);
						}
					else {
						if  (eventone.getGuestList().size() < eventone.getGuests())
							searchResultsTwo.add(eventone);
					}
				}
				HttpSession session = request.getSession(true);
				session.setAttribute("searchResults", searchResultsTwo); 
				session.setAttribute("searchTermsUserBean", event);
				session.setAttribute("searchType", searchtype);
				response.sendRedirect("searchresults.jsp");   
				return;
			}

		}
		catch (Throwable theException) 	    
		{
			System.out.println(theException); 
		}	
	}
}